import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Container;

/**
 *	Homework 10
 * MinePanel Class
 *
 * This class creates a panel for all of the buttons and tiles to be displayed on.
 * It handles all mouse events and button events with two listener classes.
 * I worked on the homework assignment alone, using only course materials.
 *
 *  @author Christopher Cheape
 *  @version 1.0 11/30/12
 */
 
public class MinePanel extends JPanel
{
	private Container buttonContainer, tileContainer;
	private GameController myController;
	private JButton restart,hint;
	
	/**
	 * Contructor for the MinePanel. It creates two containers, one for the restart and hint buttons, another for the game grid.
	 * It adds listeners to these buttons so they can work, and sets the size of the screen.
	 *
	 * @param yourController the GameController for this game.
	 */
	public MinePanel(GameController yourController)
	{
		myController=yourController; 
		restart = new JButton("RESTART");
		restart.addActionListener(new Listener());
		hint = new JButton("HINT");
		hint.addActionListener(new Listener());
		
		setLayout(new BorderLayout());
		
		buttonContainer = new Container();
		buttonContainer.setLayout(new FlowLayout());
		buttonContainer.add(restart);
		buttonContainer.add(hint);
		add(buttonContainer,BorderLayout.NORTH);
		
		tileContainer = new Container();
		tileContainer.setLayout(new GridLayout(myController.getLength(),myController.getHeight()));
		
		for(Tile[] tiles : myController.getTileList())
		{
			for(Tile tile : tiles)
			{
				tile.getButton().addActionListener(new Listener());
				tile.getButton().addMouseListener(new Mouser());
				tileContainer.add(tile.getButton());
			}
		}
		add(tileContainer,BorderLayout.CENTER);
		setPreferredSize(new Dimension(400,400));
	}
	
	/**
	 * Mouser Class
	 * A mouse listener class that checks to see if the user has right clicked. If so, it places a flag on the button that was clicked (as long as it doesnt have one already).
	 * If the button DOES already have a flag, it removes it.
	 */
	private class Mouser extends MouseAdapter
	{
		
		public void mouseClicked(MouseEvent event)
		{
			if(SwingUtilities.isRightMouseButton(event)==true)
			{
				Tile t = myController.whatWasClicked((JButton)event.getSource());
			
				if(t.gotFlag()==false)
				{
					((JButton)event.getSource()).setIcon(new ImageIcon("flag.png"));
					
					t.setFlag(true);
				}
				
				else
				{
					((JButton)event.getSource()).setIcon(null);
					
					t.setFlag(false);
				}
			}
		}
		
	}
	
	/**
	 * Listener Class
	 * An action listener class that reacts to when a button is pressed. If it was restart or hint buttons, it calls their respective methods.
	 * If it was one of the tiles, it disables it, tells the GameController, and checks to see if the user won.
	 */
	private class Listener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{  
			if((JButton)event.getSource()==restart)
			{
				Starter.tryAgain(myController);
			}
			else if((JButton)event.getSource()==hint)
			{
				myController.hint();
			}
			
			else
			{
			myController.buttonClicked((JButton)event.getSource());
			((JButton)event.getSource()).setEnabled(false);
				
			myController.checkVictory(myController.getTileList());
				
			repaint();
			}
		}
	}	
}